package info.openlvb.openface.client.handler;

import java.util.Date;

import info.openlvb.openface.client.Openface;
import info.openlvb.openface.client.images.OpenFaceImages;
import info.openlvb.openface.client.service.GraphService;
import info.openlvb.openface.client.service.GraphServiceAsync;
import com.google.gwt.core.client.*;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtextux.client.widgets.timeline.DateTime;


public class CallPersoGraphListenner implements ClickHandler, KeyUpHandler{

	private Openface openFace;
	private ClickEvent event;
	String[] arg;
	private static Image img;
	private static Date date1;
	private static Date date2;
	private static TextBox dateBox1;
	private static TextBox dateBox2;
	private static TextBox startTime;
	private static TextBox endTime;
	private static String origineImg;


	OpenFaceImages images = GWT.create(OpenFaceImages.class);

	public CallPersoGraphListenner(){ //RootPanel rootPanel, String login, String password){
	}
	@Override
	public void onClick(ClickEvent event) {
		this.event = event;
		final Window dialogBox = createDialogBox(event);
		dialogBox.show();
	}
	@Override
	public void onKeyUp(KeyUpEvent event) {
	}
	/**
	 * Create the dialog box for this example.
	 * 
	 * @return the new dialog box
	 */
	private Window createDialogBox(ClickEvent eventSource) {
		// Create a dialog box and set the caption text
		origineImg= ((Image)eventSource.getSource()).getTitle();
		final Window window = new Window();
		window.setTitle("Personnalisation des graphes");
		window.setModal(true);
		window.setClosable(true);
		window.setPlain(true);
		window.setLayout(new HorizontalLayout(15));
		img = new Image();
		img.setUrl(((Image) event.getSource()).getUrl());
		window.add(img);
		final FlexTable flexTable = new FlexTable();
		window.setCloseAction(Window.HIDE);
		flexTable.ensureDebugId("cwFlexTable");
		flexTable.setStyleName("flexTable");
		//flexTable.setBorderWidth(1);
		FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
		// Add some text
		flexTable.setHTML(0, 0, "Dates");
		cellFormatter.setStyleName(0, 0, "cellFlexTable");
		cellFormatter.setColSpan(0, 0, 4);
		flexTable.setHTML(1, 0,"Start day :");
		cellFormatter.setStyleName(1, 0, "cellFlexTable");
		cellFormatter.setColSpan(1, 0, 2);
		flexTable.setHTML(1, 1, "End day :");
		cellFormatter.setStyleName(1, 1, "cellFlexTable");
		cellFormatter.setColSpan(1, 1, 2);

		dateBox1 = new TextBox();
		date1 = new Date();
		date2 = new Date();
		dateBox1.setText(DateTimeFormat.getFormat("dd/MM/yy").format(date1));
		dateBox2 = new TextBox();
		dateBox2.setText(DateTimeFormat.getFormat("dd/MM/yy").format(date2));
		dateBox1.setWidth("80px");
		dateBox2.setWidth("80px");
		PushButton buttonDate1 = new PushButton(images.calendar().createImage());
		buttonDate1.addClickHandler(new DatePickerListenner(this,"Date1"));
		PushButton buttonDate2 = new PushButton(images.calendar().createImage());
		buttonDate2.addClickHandler(new DatePickerListenner(this,"Date2"));
		HorizontalPanel hp1 = new HorizontalPanel();
		HorizontalPanel hp2 = new HorizontalPanel();
		hp1.add(dateBox1);
		hp1.add(buttonDate1);
		hp2.add(dateBox2);
		hp2.add(buttonDate2);
		// Set the value in the text box when the user selects a date
		flexTable.setWidget(2, 0, hp1);
		cellFormatter.setColSpan(2, 0, 2);
		flexTable.setWidget(2, 1, hp2);
		cellFormatter.setColSpan(2, 1, 2);
		flexTable.setHTML(3, 0, "Times");
		cellFormatter.setStyleName(3, 0, "cellFlexTable");
		cellFormatter.setColSpan(3, 0, 4);
		startTime = new TextBox();
		endTime = new TextBox();
		startTime.setWidth("55");
		endTime.setWidth("55");
		startTime.setText(DateTimeFormat.getFormat("HH:mm").format(new Date()));
		endTime.setText(DateTimeFormat.getFormat("HH:mm").format(new Date()));
		flexTable.setHTML(4, 0, "Start time :");
		cellFormatter.setStyleName(4, 0, "cellFlexTable");
		cellFormatter.setColSpan(4, 0, 2);
		flexTable.setWidget(5, 0, startTime);
		cellFormatter.setStyleName(5, 0, "cellFlexTable");
		cellFormatter.setColSpan(5, 0, 2);
		flexTable.setHTML(4, 1, "End time :");
		cellFormatter.setStyleName(4, 1, "cellFlexTable");
		cellFormatter.setColSpan(4, 1, 2);
		flexTable.setWidget(5, 1, endTime);
		cellFormatter.setStyleName(5, 1, "cellFlexTable");
		cellFormatter.setColSpan(5, 1, 2);
		Button closeButton = new Button("CLOSE",
				new ClickHandler() {
			public void onClick(ClickEvent event) {
				window.hide();
			}
		});
		closeButton.setStyleName("graphButton");
		Button applyButton = new Button("APPLY");
		applyButton.addClickHandler(new ApplyPersoGraphListenner());

		applyButton.setStyleName("graphButton");
		VerticalPanel vp = new VerticalPanel();
		vp.add(flexTable);
		HorizontalPanel hp3 = new HorizontalPanel();
		hp3.add(applyButton);
		hp3.add(closeButton);
		vp.add(hp3);
		window.add(vp);
		return window;
	}
	private KeyUpHandler DateBoxListenner(
			CallPersoGraphListenner callPersoGraphListenner, String string) {
		// TODO Auto-generated method stub
		return null;
	}
	public static Image getIMG() {
		// TODO Auto-generated method stub
		return img;
	}
	public void setURL(String url){
		img.setUrl(url);
	}
	public static TextBox getDateBox1() {
		return dateBox1;
	}
	public void setDateBox1(TextBox dateBox1) {
		this.dateBox1 = dateBox1;
	}
	
	public static void setTextDateBox1(String date) {
		dateBox1.setValue(date,true);
	}
	
	public static void setTextDateBox2(String date) {
		dateBox2.setValue(date);
	}
	
	public static TextBox getDateBox2() {
		return dateBox2;
	}
	public void setDateBox2(TextBox dateBox2) {
		this.dateBox2 = dateBox2;
	}
	public static TextBox getStartTime() {
		return startTime;
	}
	public void setStartTime(TextBox startTime) {
		this.startTime = startTime;
	}
	public static TextBox getEndTime() {
		return endTime;
	}
	public void setEndTime(TextBox endTime) {
		this.endTime = endTime;
	}
	public static String getOrigineImg() {
		return origineImg;
	}
	public void setOrigineImg(String origineImg) {
		this.origineImg = origineImg;
	}
	public static Date getDate1(){
		return date1;
	}
	public static void setDate1(Date date){
		date1 = date;
	}
	public static Date getDate2(){
		return date2;
	}
	public void setDate2(Date date){
		this.date2 = date;
	}
}
